package com.kuyun.base.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.base.base.BaseController;
import com.kuyun.base.base.Page;
import com.kuyun.base.model.BaseGroup;
import com.kuyun.base.model.HttpResult;
import com.kuyun.base.model.ValidFormResult;
import com.kuyun.base.model.ZtreeBean;
import com.kuyun.base.util.Constants;

/**
 * 组织机构管理controller
 * 
 * @author 王坤
 *
 * @date 2016年6月19日下午00:28:37
 *
 */
@Controller
@RequestMapping("/webback/BaseGroup")
public class BaseGroupController extends BaseController {

	/**
	 * 获取组织机构树信息
	 */
	@RequiresPermissions("GROUP_LIST")
	@RequestMapping("/getBaseGroupTree.do")
	@ResponseBody
	public HttpResult getBaseGroupTree() {
		HttpResult result = new HttpResult();
		try {
			List<ZtreeBean> list = baseGroupService.getGroupTree();
			result.setObj(list);
			result.setResultCode(Constants.RESULT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setObj(null);
			result.setResultCode(Constants.RESULT_FINAL);
		}
		return result;
	}

	/**
	 * 为了用户选择而生成的tree
	 * 
	 * @author 唐靖
	 *
	 * @date 2017年7月4日下午9:51:29
	 *
	 * @return
	 */
	@RequestMapping("/getBaseGroupTreeForUser.do")
	@ResponseBody
	public HttpResult getBaseGroupTreeForUser(String userId) {
		HttpResult result = new HttpResult();
		try {
			List<ZtreeBean> list = baseGroupService.getBaseGroupTreeForUser(userId);
			result.setObj(list);
			result.setResultCode(Constants.RESULT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setObj(null);
			result.setResultCode(Constants.RESULT_FINAL);
		}
		return result;
	}

	/**
	 * 保存用户的组织机构信息
	 * 
	 * @author 唐靖
	 *
	 * @date 2017年7月4日下午9:38:33
	 *
	 * @return
	 */

	@ResponseBody
	@RequestMapping("saveUserGroup")
	public HttpResult saveUserGroup(String ids, String id) {
		HttpResult result = new HttpResult();
		try {
			baseGroupService.saveUserGroup(ids, id);
			result.setResultCode(Constants.RESULT_SUCCESS);
			result.setDesc("保存成功");
		} catch (Exception e) {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("服务器繁忙，请稍后重试");
		}
		return result;
	}

	/**
	 * 跳转字典列表
	 */
	@RequiresPermissions("GROUP_LIST")
	@RequestMapping("/toListGroup.do")
	public String toListDictionary() {
		return "group/group_list";
	}

	/**
	 * 获取父节点下字典信息
	 */
	@RequiresPermissions("GROUP_LIST")
	@ResponseBody
	@RequestMapping("/getChildrenGroup.do")
	public Page<BaseGroup> getChildrenGroup(Page<BaseGroup> page, String parentId) {
		page = baseGroupService.findByParentCode(page, parentId, null);
		return page;
	}

	/**
	 * 跳转到新增页面
	 * 
	 * @author 王坤
	 *
	 * @date 2016年6月18日下午7:27:37
	 *
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("GROUP_ADD")
	@RequestMapping("/toAddGroup.do")
	public String toAddGroup(String parentId, String level, Model model) throws Exception { // 父亲的id
		BaseGroup base = null; // 父权限
		BaseGroup typeDic = null;
		if ("root".equals(parentId) || "0".equals(level)) {
			base = new BaseGroup();
			base.setBaseGroupCode("0");
			base.setBaseGroupName("组织机构");

		} else {
			base = baseGroupService.findByCode(parentId);
		}
		model.addAttribute("base", base);
		return "group/group_edit";

	}

	/**
	 * 跳转到修改页面
	 * 
	 * @author 王坤
	 *
	 * @date 2016年6月18日下午7:27:37
	 *
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("GROUP_UPDATE")
	@RequestMapping("/toUpdateGroup.do")
	public String toUpdateGroup(String id, String parentId, Model model) throws Exception { // 父亲的id
		BaseGroup base = null; // 父权限
		BaseGroup baseDictionary = null; // 父权限
		if ("root".equals(parentId)) {
			base = new BaseGroup();
			base.setBaseGroupCode("0");
			base.setBaseGroupName("组织机构");
		} else {
			base = baseGroupService.findByID(parentId);
		}
		baseDictionary = baseGroupService.findByID(id);
		model.addAttribute("base", base);
		model.addAttribute("baseGroup", baseDictionary);
		return "group/group_edit";
	}

	/**
	 * 新增字典对象
	 */
	@RequiresPermissions("GROUP_ADD")
	@RequestMapping(value = "/addGroup.do", method = RequestMethod.POST)
	@ResponseBody
	public ValidFormResult addGroup(BaseGroup baseGroup) {
		String flag = baseGroupService.insert(baseGroup);
		ValidFormResult result = new ValidFormResult();
		if ("n".equals(flag)) {
			result.setInfo("操作有问题！大兄弟");
			result.setStatus(ValidFormResult.N);
		} else {
			result.setStatus(ValidFormResult.Y);
		}
		return result;
	}

	/**
	 * 更新字典对象
	 */
	@RequiresPermissions("GROUP_UPDATE")
	@RequestMapping("/updateGroup.do")
	@ResponseBody
	public ValidFormResult updateGroup(BaseGroup baseGroup) {
		String flag = baseGroupService.update(baseGroup);
		ValidFormResult result = new ValidFormResult();
		if ("n".equals(flag)) {
			result.setInfo("操作有问题！大兄弟");
		}
		result.setStatus(flag);
		return result;
	}

	/**
	 * 删除字典信息
	 */
	@RequiresPermissions("GROUP_UPDATE")
	@RequestMapping("/deleteGroup.do")
	@ResponseBody
	public ValidFormResult deleteGroup(String id) {
		ValidFormResult result = baseGroupService.deleteGroup(id);
		return result;
	}

	/**
	 * 判断code是否重复
	 */
	@RequestMapping("/checkGroupCode.do")
	@ResponseBody
	public ValidFormResult checkGroupCode(String param, String id) {
		ValidFormResult result = new ValidFormResult();
		BaseGroup base = new BaseGroup();
		base.setBaseGroupCode(param);
		try {
			if (baseGroupService.checkCode(base, id)) {
				result.setStatus(ValidFormResult.Y);
			} else {
				result.setInfo("该编码已经存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setInfo("系统错误，请稍后再试");
		}
		return result;
	}
}
